<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主讲人" prop="speaker">
        <el-input
          v-model="queryParams.speaker"
          placeholder="请输入主讲人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程标题" prop="videoTitle">
        <el-input
          v-model="queryParams.videoTitle"
          placeholder="请输入课程标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:CompassVideoClass:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:CompassVideoClass:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:CompassVideoClass:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:CompassVideoClass:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CompassVideoClassList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程标题" align="center" prop="videoTitle" />
      <el-table-column label="主讲人" align="center" prop="speaker" />
      <el-table-column label="主讲人简介" align="center" prop="speakerIntroduction" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:CompassVideoClass:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:CompassVideoClass:remove']"
          >删除</el-button>
          <router-link :to="{name: 'class_eva', query: {id: scope.row.id, name: scope.row.videoTitle}}" style="margin-left: 10px">
            <el-button
              size="mini"
              type="text"
            >评价</el-button>
          </router-link>
          <router-link :to="{name: 'video_list', query: {id: scope.row.id, name: scope.row.videoTitle}}" style="margin-left: 10px">
            <el-button
              size="mini"
              type="text"
            >课程目录</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1004px" append-to-body @close="$refs.editor.clearEditor()">
      <el-form ref="form" :model="form" :rules="rules" label-width="92px" :inline="true">
        <el-form-item label="课程标题" prop="videoTitle">
          <el-input v-model="form.videoTitle" placeholder="请输入课程标题" />
        </el-form-item>
        <el-form-item label="主讲人" prop="speaker">
          <el-input v-model="form.speaker" placeholder="请输入主讲人" />
        </el-form-item>
        <el-form-item label="主讲人简介" prop="speakerIntroduction">
          <el-input v-model="form.speakerIntroduction" placeholder="请输入主讲人简介" />
        </el-form-item>
        <el-form-item label="点击数量" prop="lookNum">
          <el-input v-model="form.lookNum" type="number" placeholder="请输入点击数量" />
        </el-form-item>
        <el-form-item label="点赞数量" prop="lookNum">
          <el-input v-model="form.likeNum" type="number" placeholder="请输入点赞数量" />
        </el-form-item>
        <el-form-item label="付费课程" prop="isPaid">
          <el-switch
            v-model="form.isPaid"
            :active-value="1"
            :inactive-value="0"
            active-text="付费"
            inactive-text="免费">
          </el-switch>
        </el-form-item>
        <el-form-item label="首页推荐" prop="isRecommend">
          <el-switch
            v-model="form.isRecommend"
            :active-value="1"
            :inactive-value="0"
            active-text="是"
            inactive-text="否">
          </el-switch>
        </el-form-item>
        <br>
        <el-form-item label="封面" prop="titlePage">
          <Upload v-model="form.titlePage" @clear="clearImageValidate" />
        </el-form-item>
        <el-form-item label="内容详情" prop="videoDetails">
          <Editor v-model="form.videoDetails" ref="editor"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCompassVideoClass, getCompassVideoClass, delCompassVideoClass, addCompassVideoClass, updateCompassVideoClass, exportCompassVideoClass } from "@/api/system/CompassVideoClass";
  import Editor from "@/components/Editor";
  import Upload from "@/components/Upload";

  export default {
    components: {
      Editor,
      Upload
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 课程管理表格数据
        CompassVideoClassList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          speaker: null,
          videoTitle: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          isRecommend: [
            { required: true, message: "是否首页推荐不能为空", trigger: "blur" }
          ],
          speakerIntroduction: [
            { required: true, message: "主讲人简介不能为空", trigger: "blur" }
          ],
          videoDetails: [
            { required: true, message: "内容详情不能为空", trigger: "blur" }
          ],
          isPaid: [
            { required: true, message: "是否付费课程不能为空", trigger: "blur" }
          ],
          speaker: [
            { required: true, message: "主讲人不能为空", trigger: "blur" }
          ],
          clickNum: [
            { required: true, message: "点击数量不能为空", trigger: "blur" }
          ],
          titlePage: [
            { required: true, message: "封面不能为空", trigger: "blur" }
          ],
          videoTitle: [
            { required: true, message: "课程标题不能为空", trigger: "blur" }
          ],
        },
        video_fun: 'upload'
      };
    },
    created() {
      this.getList();
    },
    methods: {
      clearImageValidate() {
        this.rules.newsCover = [];
        this.$refs["form"].clearValidate("titlePage");
      },
      /** 查询课程管理列表 */
      getList() {
        this.loading = true;
        listCompassVideoClass(this.queryParams).then(response => {
          this.CompassVideoClassList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          isRecommend: null,
          speakerIntroduction: null,
          videoDetails: null,
          isPaid: null,
          speaker: null,
          clickNum: null,
          titlePage: null,
          videoTitle: null,
          updateTime: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加课程管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getCompassVideoClass(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改课程管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          const postData = {
            ...this.form,
            videoDetails: this.filterContent(this.form.videoDetails)
          };
          if (valid) {
            if (this.form.id != null) {
              updateCompassVideoClass(postData).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addCompassVideoClass(postData).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除课程管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompassVideoClass(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有课程管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCompassVideoClass(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  };
</script>
